<?php

class Application_Model_Auth
{
private $_auth;

	public function __construct () {
		$this->_auth = Zend_Auth::getInstance ();
	}

	//帐号密码登录
	public function authorise($phone, $pwd) {
		if (empty($phone) || empty($pwd))
			return false;


        $dbAdapter = Zend_DB_TABLE::getDefaultAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable ($dbAdapter);
        $authAdapter->setTableName ('t_enterprise_user')
            ->setIdentityColumn ('account')
            ->setCredentialColumn ('pwd');
        $authAdapter->setIdentity ($phone)
            ->setCredential (md5($pwd));
        $result = $this->_auth->authenticate ($authAdapter);
        if ($result->isValid()){
            $userInfo = $authAdapter->getResultRowObject(array('id','account','enterprise_id','enterprise_role_id','tel','pwd','is_del'));
            $enterpriseMapper = new Application_Model_EnterpriseMapper();
            $enterprise_info = $enterpriseMapper->getEnterpriseInfo($userInfo->enterprise_id);
            if($enterprise_info['0']['audit_status'] == 1){
                return 'error';
            }else{
                $this->_auth->getStorage()->write($userInfo);
                return 'success';
            }
        }
        return false;
	}

	public function isLoggedIn() {
		return $this->_auth->hasIdentity();
	}

	public function logout() {
		$this->_auth->clearIdentity();
		return $this;
	}

	public function getId () {
		if (!$this->_auth->hasIdentity()){
			return null;
		}
		return $this->_auth->getStorage()->read()->id;
	}
	public function getis_del () {
		if (!$this->_auth->hasIdentity()){
			return null;
		}
		return $this->_auth->getStorage()->read()->is_del;
	}

	public function getPwd () {	
		if (!$this->_auth->hasIdentity()){
			return null;
		}
		return $this->_auth->getStorage()->read()->pwd;
	}
	public function getAccount () {
		if (!$this->_auth->hasIdentity()){
			return null;
		}
		return $this->_auth->getStorage()->read()->account;
	}

	public function getEnterpriseId () {
		if (!$this->_auth->hasIdentity()){
			return null;
		}
		return $this->_auth->getStorage()->read()->enterprise_id;
	}

	public function getEnterpriseRoleId () {
		if (!$this->_auth->hasIdentity()){
			return null;
		}
		return $this->_auth->getStorage()->read()->enterprise_role_id;
	}

	public function getTel () {
		if (!$this->_auth->hasIdentity()){
			return null;
		}
		return $this->_auth->getStorage()->read()->tel;
	}

}

